package calculate24.play.AutoCalculate;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.content.res.AppCompatResources;

import calculate24.play.R;
import calculate24.play.common.calculate24.Calculate24;
import calculate24.play.common.card.Card;
import calculate24.play.common.card.DrawTextImageView;
import calculate24.play.common.card.Poker;

//用户选四张牌后，自动算出所有可能的解
public class AutoCalculate extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //展示选中的牌
        PickedCards pickedCards = new PickedCards(
                findViewById(R.id.pickedCard1),
                findViewById(R.id.pickedCard2),
                findViewById(R.id.pickedCard3),
                findViewById(R.id.pickedCard4));
        TextView prompt = findViewById(R.id.prompt); //提示结果数量
        LinearLayout resultZone = findViewById(R.id.resultZone); //展示结果
        FrameLayout cardsZone = findViewById(R.id.cardsZone);//供用户选扑克牌的区域

        //放入全部52张扑克牌
        final int[][] poker = new int[][] {Poker.spades, Poker.clubs, Poker.hearts, Poker.diamonds};
        final int cardHeight = cardsZone.getLayoutParams().height / 3;
        final int cardWidth = 3 * cardHeight / 4;
        final int deltaLeftMargin = cardWidth / 3;
        final int deltaTopMargin = 3 * cardHeight / 5;
        final int initialTopMargin = cardHeight / 5;
        final int oddInitialLeftMargin = -cardWidth / 10;
        final int evenInitialLeftMargin = cardWidth / 10;
        final int pickDeltaTopMargin = -cardHeight / 5;
        final Drawable highLightFrame = AppCompatResources.getDrawable(this, R.drawable.high_light_frame);
        for(int i = 0; i < 4; ++i) {
            int topMargin = i * deltaTopMargin + initialTopMargin;
            int initialLeftMargin = i % 2 == 0? oddInitialLeftMargin : evenInitialLeftMargin;
            for(int j = 1; j <= 13; ++j) {
                int leftMargin = j * deltaLeftMargin + initialLeftMargin;
                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(cardWidth, cardHeight);
                layoutParams.topMargin = topMargin;
                layoutParams.leftMargin = leftMargin;
                DrawTextImageView cardView = new DrawTextImageView(this);
                cardView.setImageResource(poker[i][j]);
                Card card = new Card(cardView, j);
                card.setOnPickListener(new Card.OnPickListener() {
                    @Override
                    public void onPick(Card card) {
                        pickedCards.add(card);
                        layoutParams.topMargin += pickDeltaTopMargin;
                        card.view.setForeground(highLightFrame);
                        if(pickedCards.isFull()) {
                            //执行运算，展示结果
                            Calculate24 cal = new Calculate24(pickedCards.allNumbers());
                            StringBuilder sb = new StringBuilder();
                            if(cal.resultSet.size() == 0) {
                                prompt.setText(R.string.no_solution_prompt);
                            } else {
                                prompt.setText(getString(R.string.solutions_prompt, cal.resultSet.size()));
                                for(int i = 0; i < cal.resultSet.size(); ++i) {
                                    sb.append(getString(R.string.solution, i + 1, cal.resultSet.get(i)));
                                    sb.append('\n');
                                }
                            }
                            TextView textView = new TextView(AutoCalculate.this);
                            textView.setText(sb);
                            textView.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
                            textView.setTextAppearance(R.style.resultStyle);
                            resultZone.addView(textView);
                        }
                    }

                    @Override
                    public void onDrop(Card card) {
                        pickedCards.remove(card);
                        layoutParams.topMargin -= pickDeltaTopMargin;
                        card.view.setForeground(null);
                        prompt.setText(R.string.initial_prompt);
                        resultZone.removeAllViews();
                    }

                    @Override
                    public boolean canPick(Card card) {
                        return !pickedCards.isFull();
                    }
                });
                cardsZone.addView(card.view, layoutParams);
            }
        }

    }
}